import grails.util.Metadata
import grame.AppSetting
import grame.AppUpgrade
import grame.GrameInit

import javax.servlet.ServletContext

/**
 * @author Shining Wang <shiningwang@topca.com>
 */
class BootStrap {
    def shiroSecurityService;

    def init = { servletContext ->
        println "init..."
        String appName = Metadata.getCurrent().getApplicationName()
        String appVersion = Metadata.getCurrent().getApplicationVersion()
        String appDataVersion = AppSetting.findByName('app.version')?.value

        println "${appName} bootstrap..."
        println "${appName} current version: ${appDataVersion ?: 0.1}"
        println "${appName} release version: ${appVersion}"

        // upgrade application
        if (!appDataVersion) {
            AppSetting.findOrCreateByName('app.version')
                    .setValue(appVersion)
                    .save(flush: true)
        }
        AppUpgrade.run((ServletContext) servletContext);


        GrameInit.init(shiroSecurityService)

        println "${appName} running..."
    }
    def destroy = {
        println "destroy..."
    }
}
